<template lang="pug">

.content
    .FriendsList
        table Firends List
            tr
                td Email
                td display
                td create_time

            tr(v-for="(item,index) in ownFriendsResult")
                td {{item.email}}
                td {{item.display}}
                td {{item.create_time}}


    .findUser
        label Find Other Users
        input(v-model="keyWord"  v-on:keyup.enter="SearchFriends" placeholder="input user`s display")
        table
            tr
                td Email
                td display
                td create_time
                td state
                td Add SomeOne
            tr(v-for="(item,index) in searchResult" :key="index" v-show="IsNotSelf(item.email)")
                td {{item.email}}
                td {{item.display}}
                td {{item.create_time}}
                td(:style="{color: item.disabled?'#fc4c4c':'#1cef17'}") {{item.disabled? 'Disable':'Enable' }}
                td
                    span(@click="AddFriends(item.user_id)"  v-show="item.email!=='root'&&item.email!== DataStoreUserInfo?.email") AddFriends

</template>

<script lang="ts" setup>

import {onMounted, ref} from "vue";
import type {UserInfo} from "../../utils/types/UserInfo";
import {AdminUserInfoByDisplay} from "../../reqApi/AdminApi";
import type {ResultInfo} from "../../utils/types/ResultInfo";
import {LogSuccess} from "../../utils/notification/Index";
import {UserFriendsGet, UserFriendsReq} from "../../reqApi/UserApi";
import {StoreUserInfo} from "../../store/userInfo";

const ownFriendsResult = ref<UserInfo[]>([])
const DataStoreUserInfo = StoreUserInfo().Get()
const keyWord = ref("")
const searchResult = ref<UserInfo[]>([])
const IsNotSelf = (email: string) => {
    return email !== DataStoreUserInfo.value?.email
}
const SearchFriends = () => {
    AdminUserInfoByDisplay({
        display: keyWord.value
    }).then(resp => {
        const result = resp.data as ResultInfo<UserInfo[]>
        LogSuccess(result.msg)
        if (result.success) {
            searchResult.value = result.data || []
        }
    })
}
// AdminUpdateUserInfo
const AddFriends = (userId: number) => {
    for (let valueElement of ownFriendsResult.value) {
        if (valueElement.user_id == userId) {
            LogSuccess("已经添加其为好友")
            return
        }
    }
    UserFriendsReq({
        user_id: userId
    }).then(resp => {
        const result = resp.data as ResultInfo<any>
        LogSuccess(result.msg)

    })
}

onMounted(() => {
    UserFriendsGet().then(resp => {
        const result = resp.data as ResultInfo<UserInfo[]>
        ownFriendsResult.value = result.data || []
        LogSuccess(result.msg)
    })
})
</script>

<style scoped lang="sass">
@import "../../assets/sass/user/friends"
</style>


}
